package com.huike.web.controller.clues;


import com.huike.business.service.ITbBusinessTrackRecordService;
import com.huike.clues.domain.TbClue;
import com.huike.clues.domain.TbClueTrackRecord;
import com.huike.clues.domain.vo.ClueTrackRecordVo;
import com.huike.common.annotation.Log;
import com.huike.common.core.controller.BaseController;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.core.page.TableDataInfo;
import com.huike.common.enums.BusinessType;
import com.huike.common.utils.DateUtils;
import com.huike.common.utils.SecurityUtils;
import com.huike.common.utils.bean.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;


/**
 * 线索跟进记录Controller
 * @date 2021-04-22
 */
@RestController
@RequestMapping("/clues/record")
public class TbClueTrackRecordController extends BaseController {

    @Autowired
    private ITbBusinessTrackRecordService service;

    /**
     * 查询线索跟进记录列表
     */
    @PreAuthorize("@ss.hasPermi('clues:record:list')")
    @GetMapping("/list")
    public TableDataInfo list(@RequestParam("clueId")Long clueId) {
        List<TbClueTrackRecord> list =service.selectAllByClueId(clueId);
       return getDataTable(list);
    }


    /**
     * 新增线索跟进记录
     */
    @PreAuthorize("@ss.hasPermi('clues:record:add')")
    @Log(title = "线索跟进记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ClueTrackRecordVo tbClueTrackRecord) {
        //更新线索表
        TbClue clue=new TbClue();
        BeanUtils.copyBeanProp(clue,tbClueTrackRecord);
        //id不会copy，需要单独处理
        clue.setId(tbClueTrackRecord.getClueId());

        clue.setUpdateBy(SecurityUtils.getUsername());
        clue.setUpdateTime(new Date());
        //更新 状态 待跟进到已跟进
        clue.setStatus(TbClue.StatusType.FOLLOWING.getValue());

        //插入一条跟进记录
        TbClueTrackRecord record = new TbClueTrackRecord();

        BeanUtils.copyBeanProp(record,tbClueTrackRecord);
        record.setCreateBy(SecurityUtils.getUsername());
        record.setCreateTime(new Date());

        int i =service.insertTbClueTrackRecordAndupdateTbClue(clue,record);

        return toAjax(i);
    }
}
